﻿using System;
using ApplicationGenerator.BusinessModule.Elements;
using ApplicationGenerator.BusinessModule.MetaElements;
using ApplicationGenerator.BusinessModule.Interfaces;

namespace ApplicationGenerator.Forms.Utilities
{
    public class PathToMetaAdapterConverter
    {
        public static IMetaModel ConvertToMetaAdapter(string path)
        {
            IMetaModelCollection metaAdapterCollection = (new FacadeFactory()).CreateMetaModelFacade().MetaModelCollection;
            string[] pathParts = path.Split(new char[] { '.' });
            if (pathParts.Length < 2)
            {
                throw new Exception("Niepoprawna ścieżka: " + path);
            }

            IMetaModel metaAdapter = metaAdapterCollection[pathParts[pathParts.Length - 2]];
            if (metaAdapter == null)
            {
                throw new Exception("Nieprawidłowa nazwa modelu w ścieżce: " + path);
            }

            return metaAdapter;
        }
    }
}